package com.roc.service.orgmain;

import java.util.ArrayList;

import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.roc.bean.AreaBean;
import com.roc.bean.orgMainBean;
import com.roc.dao.DaoException;
import com.roc.dao.IBaseDao;
import com.roc.dao.ISQLBaseDao;
import com.roc.newPage.IPageDao;
import com.roc.pojo.Operators;
import com.roc.service.ServiceException;
import com.roc.util.Constance;
import com.roc.util.constant.SysLogType;
@Service
@Transactional
public class OrgMainServiceImpl implements  IOrgMainService{

	static Logger logger=Logger.getLogger(OrgMainServiceImpl.class);

	@Autowired
	private IBaseDao baseDao;
	@Autowired
	private ISQLBaseDao sqlBaseDao;
	@Autowired
	private IPageDao pageDao;
	
	
	@Override
	public boolean addOrgList(orgMainBean org, Operators op) throws ServiceException {
	
		String sql =" SELECT COUNT(1) FROM TBL_ORG_MAIN WHERE  org_name =? ";
		List<Object>  params = new ArrayList<Object>();
		String[] parma = {org.getOrgName()};
		params.add(org.getOrgName());
		try {
			Object r = pageDao.findObj(sql, parma);
			long count = Long.parseLong(r.toString());
			if(count>0){
				return false;
			}else{
				String sql1=" insert into TBL_ORG_MAIN    "
						+"  (org_code,org_name, area_id,super_id,crt_ts,opr_id,status) "
						+" values  "
						+"  (seq_tbl_org_main_org_code.nextval,?,?,?,sysdate,?,?)";
				List<Object> parmas=new ArrayList<Object>();
				parmas.add(org.getOrgName());
				parmas.add(org.getAreaId());
				parmas.add(op.getOrgCode());
				parmas.add(String.valueOf(op.getId()));
				parmas.add(org.getStatus());
				int ex=sqlBaseDao.deleteBySql(sql1, parmas);
				if(ex>0){
				logger.info("添加收单机构"+op.getOrgCode()+"下级机构"+org.getOrgName()+"】成功!");
				baseDao.addSyslog(SysLogType.ADD_ORG.getCode(), SysLogType.ADD_ORG.getName(), op.getAccountNo(), op.getAccountNo()+"添加收单机构"+op.getOrgCode()+"下级机构"+org.getOrgName()+"】成功");
				}
			}
		} catch (DaoException e) {
			e.printStackTrace();
			logger.debug(e.getMessage(), e);
			throw new ServiceException("插入机构失败！！");
		
		}
		return true;
	}
	/**
	 * 验证是否存在
	 * @see com.roc.service.area.IAreaService#findByAreaNum(java.lang.String)
	 * @Title: findByAreaNum
	 * @Description:TODO(...)
	 * @param areaNum
	 * @return
	 * @throws ServiceException
	 * @author JD
	 * @date: 2017年8月7日 下午3:50:20
	 *
	 */
	@Override
	public orgMainBean findOrgByCode(String id) throws ServiceException {
		String sql=" select t.org_code,t.org_name,"+
			       "(select a.name from tbl_area a where a.id = t.area_id) area_name,"+
			       "(select i.ins_name from tbl_ins_inf i where i.ins_id_cd = t.super_id) sd_org_name,"+
			       "(select o.real_name from t_operators o where to_char(o.id) = t.opr_id) opr_name,"+
			       "t.crt_ts   from TBL_ORG_MAIN t where 1 = 1 and t.org_code=?";
		Object[] parmas={id};
		orgMainBean org=null;
		try {
			Object object=sqlBaseDao.findObj(sql, parmas);
			if(object!=null)
			{
				Object[] obj=(Object[]) object;
				org=new orgMainBean();
				org.setOrgCode(Constance.Null2EmptyTrim(obj[0]));
				org.setOrgName(Constance.Null2EmptyTrim(obj[1]));
				org.setAreaName(Constance.Null2EmptyTrim(obj[2]));
				org.setSuperName(Constance.Null2EmptyTrim(obj[3]));
				org.setOprName(Constance.Null2EmptyTrim(obj[4]));
				org.setCrtTs(Constance.Null2EmptyTrim(obj[5]));
			}
		} catch (DaoException e) {
			logger.debug(e.getMessage(),e);
			throw new ServiceException("查询区域id为【"+id+"】失败");
		}
		return org;
	}

	@Override
	public int updateOrgList(orgMainBean org, Operators op)
			throws ServiceException {
		String sql=" UPDATE  TBL_ORG_MAIN t SET  t.org_name=?,  t.area_id=?,  t.super_id=?, t.status=? , t.up_ts=sysdate ,t.UPT_OPR=?  WHERE t.org_code=? ";
		int effect=0;
		List<Object> parmas=new ArrayList<Object>();
		parmas.add(org.getOrgName());
		parmas.add(org.getAreaId());
		parmas.add(op.getOrgCode());
		parmas.add(org.getStatus());
		parmas.add(op.getId());
		parmas.add(org.getOrgCode());
		try {
			effect=sqlBaseDao.deleteBySql(sql, parmas);
			if(effect>0)
			{
				logger.info("更改【"+"机构"+org.getOrgCode()+"信息成功!");		
				baseDao.addSyslog(SysLogType.UPDATE_ARG.getCode(), SysLogType.UPDATE_ARG.getName(), op.getAccountNo(), op.getAccountNo()+"更改机构"+org.getOrgCode()+"信息成功");
			}
		} catch (DaoException e) {
			logger.debug(e.getMessage(),e);
			throw new ServiceException("更改机构信息失败");
		}
		return effect;
	}
	/**
	 * 查询所有区域
	 */
	@Override
	public List<AreaBean> queryAreaInf(Operators op) throws ServiceException {
		
		 List<AreaBean> areaCodeList = new ArrayList<AreaBean>();
			try {
				String sql="";
				 sql=" SELECT  t.id,t.name FROM  tbl_area  t  WHERE   t.ins_code = '" + op.getOrgCode() + "'  AND t.status='1'  ";
				List<?> lists = sqlBaseDao.queryAll(sql);
				
	          if(null != lists && lists.size() > 0){
	        	  Iterator iterator=lists.iterator();
	        	  while (iterator.hasNext()) {
						Object[] obj=(Object[]) iterator.next();
						AreaBean exp=new AreaBean();
						exp.setId(Constance.Null2EmptyTrim(obj[0]));
						exp.setName(Constance.Null2EmptyTrim(obj[1]));
						areaCodeList.add(exp);
				}
				}
			} catch (Exception e) {
				e.printStackTrace();
				logger.error("加载区域失败", e);
			}	
			return areaCodeList;
		}

}
